package com.inibb.app.util.ioc;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


/**
 *  
 * Container for bean init by Spring framework 
 * 
 * @author anselm
 * @since  Oct 28, 2009
 * @version 1.0
 */
public class SpringContainer implements ApplicationContextAware {
	   private static final Object mutex=new Object();
	   private static volatile SpringContainer springC;
	   
	   /**
	    * Create instance of Spring Container should be called by 
	    * Spring framework only.
	    * @return New instance of SpringContainer
	    */
	   public static SpringContainer createInstance(){
	      if(springC==null){
	         synchronized (mutex)
	         {
	            springC=new SpringContainer();
	         }
	      }
	      return springC;
	   }
	   
	   /**
	    * @return Init instance of SpringContainer
	    */
	   public static SpringContainer getContainer(){
	      return springC;
	   }
	   
	   private ApplicationContext appCtx;
	   
	   /**
	    * Check to seek contain bean
	    * @param id Bean id
	    * @return true if contain bean
	    */
	   public boolean containBean(String id){
	      return getAppCtx().containsBean(id);
	   }

	   /**
	    * Return the bean contained with container
	    * @param id Reference to the bean
	    * @return Bean instance
	    */
	   public Object getBean(String id){
	      return getAppCtx().getBean(id);
	   }   
	   
	   public void setApplicationContext(ApplicationContext applicationContext)
	         throws BeansException
	   {
	      this.appCtx = applicationContext;
	   }
	   
	   /**
	    * @return the value for appCtx
	    */
	   private ApplicationContext getAppCtx()
	   {
	      return appCtx;
	   }
}
